<?php
/**
 * User Model.
 * @package    sitrack 
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright (c) 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * User Model.
 *
 * @package    sitrack 
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright (c) 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
final class User extends SPF_Model {
   
   /**
    * Constructor.
    *
    * @return  object
    */
   public function __construct() {

      /* construct the parent object */
      parent::__construct('users');
      
      $this->add_validation('user_name', true, 'alphanumeric', '_', 2);
      $this->add_validation('display_name', true, 'alphanumeric', ' ');
      $this->add_validation('email', true, 'email', ' ');
      
   } // __construct

   /**
    * Destructor.
    *
    * @return  void
    */
   public function __destruct() {
      parent::__destruct();
   }
   
   /**
    * Override the before_update() method to convert the users password to a hash if it isn't already.
    *
    * @param   boolean   $new    true if the record was inserted.
    * @return  void
    */
   public function before_update( $new ) {
   
      // password validation...
      if( strlen($this->password) != SPF::HASH_LEN ) {
         $this->password = SPF::hash($this->password);
      }
      
   } // before_update
   
   /**
    * Override the validate() method to validate that the password and confirm_pw properties match.
    *
    * @return  void
    */
   public function validate() {
      
      // default validation
      parent::validate();
      
      // email address can only exist once
      
      // password validation...
      if( strlen($this->password) != SPF::HASH_LEN ) {
         
         if( !isset($this->confirm_pw) )
            $this->confirm_pw = NULL;
         
         if( !isset($this->errors['password']) && !$this->password ) {
            $this->errors['password'] = 'Required.';
         }
         
         if( !isset($this->errors['password']) && ($this->password != $this->confirm_pw) ) {
            $this->errors['confirm_pw'] = 'Passwords do not match.';
         }
         
      }
      
   } // validate
   
	/**
	 * Returns the status of the user with the specified user name and password.
	 *
	 * @param   string    $user_name   user to activate.
	 * @param   string    $password    hash of the users password.
    * @return  string
	 */
   public static function signin( $user_name, $password ) {
      
		$user_name = SPF::$db->escape($user_name);
		$password  = SPF::$db->escape($password);
		
		return SPF::$db->get_field('users', 'user_status', "user_name = '$user_name' AND password  = '$password'");
      
   } // signin
   
	/**
	 * Updates the status of the specified user to 'ACTIVE'.
	 *
	 * @param   string    $user_name   user to activate.
	 * @param   string    $password    hash of the users password.
    * @return  boolean
	 */
   public static function activate( $user_name, $password ) {
      
		$user_name = SPF::$db->escape($user_name);
		$password  = SPF::$db->escape($password);
		
		SPF::$db->update_field('users', 'user_status', '\'ACTIVE\'', "user_name = '$user_name' AND password = '$password' AND user_status != 'DELETED'");
		
      return SPF::$db->affected_rows() == 1;

   } // activate
   
	/**
	 * Returns an array of user data.
	 *
    * @return  array
	 */
   public static function browse_data() {
      
      $sql = "SELECT user_name,
                     display_name,
                     email,
                     user_status
                FROM users
            ORDER BY user_status,
                     user_name";
      return SPF::$db->get_all($sql);
   
   } // browse_data
   
	/**
	 * Returns an array of user statuses and the number of users with each status.
	 *
    * @return  array
	 */
   public static function get_count_by_status() {
      
      $sql = "SELECT user_status,
                     COUNT(*) AS user_count
                FROM users
            GROUP BY user_status";
      $rs = SPF::$db->get_all($sql);
      
      $data = array();
      foreach( $rs as $rec ) {
         $data[$rec['user_status']] = $rec['user_count'];
      }
      
      $enums = SPF::$db->get_enums('users', 'user_status');
      foreach( $enums as $status ) {
         if( !isset($data[$status]) )
            $data[$status] = 0;
      }
      
      return $data;
      
   } // get_count_by_status
   
	/**
	 * Returns an array of user statuses.
	 *
    * @return  array
	 */
   public static function lookup_status_options() {
      
      return SPF::$db->get_enums('users', 'user_status');
      
   } // lookup_status_options
   
	/**
	 * Returns an array of user names and display names used to create html select options.
	 *
	 * @param   string    $current_user   the current user name if any.
	 * @param   string    $status         if specified the users returned will have this status.
    * @return  array
	 */
   public static function lookup_options( $current_user = '', $status = '' ) {
      
      if( !$status )
         $status = 'ACTIVE';
      
		$status = SPF::$db->escape($status);
		
      $sql = "SELECT user_name, display_name
                FROM users
               WHERE user_status = '{$status}'\n";

      if( $current_user ) {
         $current_user = SPF::$db->escape($current_user);
			$sql .= " OR user_name = '{$current_user}'\n";
      }
      
      $sql .= ' ORDER BY display_name';
      
      $rs = SPF::$db->get_all($sql);
      
      $arr = array();
      foreach( $rs as &$row ) {
         $arr[ $row['user_name'] ] = $row['display_name'];
      }
      
      return $arr;
      
   } // lookup_options

} // User

?>
